cmake_minimum_required(VERSION 3.10)
project(reverse_proxy_poco CXX)

message(STATUS "Running on ${CMAKE_SYSTEM_NAME}")

# set(POCO_SRC_DIR /home/admin/source/poco)
# set(POCO_INCLUDE ${POCO_SRC_DIR}/include)

# include_directories(/mingw64/include)
# include_directories(${POCO_INCLUDE})
SET(CMAKE_BUILD_TYPE Debug)
# if (NOT CMAKE_BUILD_TYPE)
#     message(STATUS "CMAKE_BUILD_TYPE is not set. Defaulting to Debug.")
#     set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel.")
# endif()

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
    message(STATUS "Building in Debug mode.")
    # 在Debug模式下添加的特定设置，例如：
    add_compile_options(-g) # 添加调试信息
    add_compile_options(-DDEBUG) # 定义DEBUG宏
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Building in Release mode.")
    # 在Release模式下添加的特定设置，例如：
    add_compile_options(-O3 -DNDEBUG) # 优化代码，禁用断言
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Building in Release with Debug Info mode.")
    add_compile_options(-O2 -g -DNDEBUG)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Building in Minimum Size Release mode.")
    add_compile_options(-Os -DNDEBUG)
else()
    message(WARNING "Unknown CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}. Using default compiler flags.")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    # 设置 C++ 标准
    set(CMAKE_CXX_STANDARD 23)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)

    set(CMAKE_TOOLCHAIN_FILE c:/vcpkg/scripts/buildsystems/vcpkg.cmake)
    # set(CMAKE_PREFIX_PATH c:/vcpkg/installed/x64-windows)
    set(CMAKE_INSTALLED_PATH c:/vcpkg/installed/x64-windows)
    set(CMAKE_PREFIX_PATH ${VCPKG_INSTALLED_DIR}/share)
    # set(PocoFoundation_DIR ${CMAKE_INSTALLED_PATH}/share/poco)
    # set(Poco_DIR ${CMAKE_INSTALLED_PATH}/share/poco)
    # set(OpenSSL_DIR ${CMAKE_INSTALLED_PATH}/share/OpenSSL)

    include(${CMAKE_INSTALLED_PATH}/share/OpenSSL/OpenSSLConfig.cmake)
    include(${CMAKE_INSTALLED_PATH}/share/poco/PocoConfig.cmake)
    include(${CMAKE_INSTALLED_PATH}/share/poco/PocoFoundationConfig.cmake)
    include(${CMAKE_INSTALLED_PATH}/share/poco/PocoNetSSLConfig.cmake)
    include(${CMAKE_INSTALLED_PATH}/share/poco/PocoCryptoConfig.cmake)

    # Find POCO libraries
    # find_package(Poco REQUIRED COMPONENTS Foundation Crypto Util NetSSL OpenSSL)
elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
    set(CMAKE_CXX_STANDARD 23)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS OFF) # Ensure C++ standard adherence
    include_directories(/mingw64/include /mingw64/include/c++/15.1.0)
    set(ZLIB_LIBRARY /mingw64/lib)
    set(ZLIB_INCLUDE_DIR /mingw64/include)

    set(PCRE2_LIBRARY /mingw64/lib)
    set(PCRE2_INCLUDE_DIR /mingw64/include)

    set(OPENSSL_ROOT_DIR /mingw64)
    set(OPENSSL_CRYPTO_LIBRARY /mingw64/lib)
    set(OPENSSL_INCLUDE_DIR /mingw64/include)

    set(Poco_DIR "/mingw64/lib/cmake/Poco") # <--- Adjust this path!
    find_package(Poco REQUIRED COMPONENTS Foundation Net NetSSL)

    # include_directories(/clang64/include)
    # link_directories(/clang64/lib)
    # set(ZLIB_INCLUDE_DIR /clang64/include/zlib.h)
    # set(ZLIB_LIBRARY /clang64/lib)
    # set(PCRE2_INCLUDE_DIR /clang64/include/pcre2.h)
    # set(PCRE2_LIBRARY /clang64/lib)
    # set(OPENSSL_INCLUDE_DIR /clang64/include)
    # set(OPENSSL_CRYPTO_LIBRARY /clang64/lib)
    # find_path(ZLIB /clang64)
    # find_library(ZLIB REQUIRED)

    # find_package(ZLIB REQUIRED)
    # find_package(Poco REQUIRED COMPONENTS Foundation Net Util NetSSL OpenSSL Crypto Data)
endif()

# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/foundation/CMakeListss.txt")
# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/encodings/CMakeListss.txt")
# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/crypto/CMakeListss.txt")
# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/NetSSL_OPENSSL/CMakeListss.txt")
# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/Util/CMakeListss.txt")
# include("${CMAKE_CURRENT_SOURCE_DIR}/../poco/NET/CMakeListss.txt")

set(SRC_FILES 
    src/main.cpp
    src/ReverseProxyServer.cpp
    src/HTTPSessionHandler.cpp
    src/WebSocketSessionHandler.cpp
)
# set(SRC_FILES 
    # src/ssl_echo.cpp
    # ${POCO_FOUNDATION_SRCS}
    # ${POCO_ENCODINGS_SRCS}
    # ${POCO_CRYPTO_SRCS}
    # ${POCO_NETSSL_OPENSSL_SRCS}
    # ${POCO_UTIL_SRCS}
    # ${POCO_NET_SRCS}
# )
# Add source files
add_executable(reverse_proxy_poco
    ${SRC_FILES}
    # ${POCO_SRC_FILES}
    # ${POCO_FOUNDATION_SOURCE}
    # ${POCO_UTIL_SOURCE}
    # ${POCO_NET_SOURCE}
    # ${POCO_CRYPTO_SOURCE}
)

add_executable(echo src/ssl_echo.cpp)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    # Link POCO libraries
    # target_link_libraries(reverse_proxy_poco PRIVATE
    #     ZLIB 
    #     Poco::Foundation
    #     # Poco::Net
    #     Poco::Crypto
    #     Poco::Util
    #     Poco::NetSSL
    #     Poco::OpenSSL # Make sure this is linked if using NetSSL. It typically is transitive.
    # )

    # Set include directories if Poco was not installed to standard paths
    # include_directories(${POCO_INCLUDE_DIR}) # This might be set by find_package already

    # Set properties for C++17 or later
    # target_compile_features(reverse_proxy_poco PRIVATE cxx_23)
    target_include_directories(reverse_proxy_poco PRIVATE C:\\vcpkg\\installed\\x64-windows\\include)
    target_include_directories(echo PRIVATE C:\\vcpkg\\installed\\x64-windows\\include)
    set_property(TARGET reverse_proxy_poco PROPERTY CXX_STANDARD 23)
    set_property(TARGET reverse_proxy_poco PROPERTY CXX_STANDARD_REQUIRED ON)
    set_property(TARGET reverse_proxy_poco PROPERTY CXX_EXTENSIONS OFF)
    set_property(TARGET echo PROPERTY CXX_STANDARD 23)
    set_property(TARGET echo PROPERTY CXX_STANDARD_REQUIRED ON)
    set_property(TARGET echo PROPERTY CXX_EXTENSIONS OFF)

    message("CMAKE_BUILD_TYPE:${CMAKE_BUILD_TYPE}")
    if (CMAKE_BUILD_TYPE STREQUAL "Release")
        target_link_directories(reverse_proxy_poco PRIVATE C:\\vcpkg\\installed\\x64-windows\\lib)
        target_link_directories(echo PRIVATE C:\\vcpkg\\installed\\x64-windows\\lib)
        set(POCO_LIBS zlib.lib PocoCrypto.lib PocoFoundation.lib PocoEncodings.lib PocoNet.lib PocoNetSSL.lib PocoUtil.lib)
    else()
        target_link_directories(reverse_proxy_poco PRIVATE C:\\vcpkg\\installed\\x64-windows\\debug\\lib)
        target_link_directories(echo PRIVATE C:\\vcpkg\\installed\\x64-windows\\debug\\lib)
        set(POCO_LIBS zlibd.lib PocoCryptod.lib PocoFoundationd.lib PocoEncodingsd.lib PocoNetd.lib PocoNetSSLd.lib PocoUtild.lib)
    endif()
    target_link_libraries(reverse_proxy_poco PRIVATE libssl.lib libcrypto.lib ${POCO_LIBS})
    target_link_libraries(echo PRIVATE libssl.lib libcrypto.lib ${POCO_LIBS})

    # For Windows, you might need to specify Runtime Library
    if (MSVC)
        # target_compile_options(reverse_proxy_poco PRIVATE /MT) # Or /MD for dynamic runtime
    endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
    # target_link_libraries(reverse_proxy_poco PRIVATE
    #     Poco::Foundation
    #     Poco::Net
    #     Poco::Util
    #     Poco::NetSSL
    #     Poco::OpenSSL
    #     Poco::Crypto # Explicitly link Crypto
    #     # Poco::Data   # Explicitly link Data
    #     # --- Potentially crucial additions below ---
    #     # Link the C++ standard library explicitly.
    #     # For Clang on MSYS2, it's often libstdc++, which is named 'stdc++'
    #     # If it's libc++, it might be 'c++'. Try 'stdc++' first.
    #     stdc++
    #     # If you get more linker errors about 'ws2_32' or 'advapi32', add these
    #     ws2_32
    #     advapi32
    # )
    target_link_directories(reverse_proxy_poco PRIVATE /mingw64/lib)
    # target_compile_definitions(reverse_proxy_poco PRIVATE -DPOCO_INT64_IS_LONG -DPOCO_NO_THREADNAME)
    # target_link_libraries(reverse_proxy_poco PRIVATE ssl.dll.a crypto.dll.a stdc++)
    target_link_libraries(reverse_proxy_poco PRIVATE libssl.dll.a libcrypto.dll.a libz.dll.a)
    target_link_libraries(reverse_proxy_poco PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
    target_link_libraries(reverse_proxy_poco PRIVATE ws2_32 crypt32)
    # target_link_libraries(reverse_proxy_poco PRIVATE libPocoFoundation.a libPocoCrypto.a libPocoNetSSL.a libPocoUtil.a libssl.a libcrypto.a)
endif()